/*
 * 分类弹窗
 * 功能：显示子分类
 */
(function(factory){
    if(typeof define === 'function' && define.amd){
        define(['jquery','../../../Common/catedb','select'],factory);
    }else{
        factory(window.jQuery);
    }
}(function($){
    /*省市县=====================================*/
    $.fn.areaB = function(o,callback) {
        var that = $.extend({},$.fn.areaB.defaults, o);
        return this.each(function() {
            var t = $(this);
            var arthtml='<select name="'+that.a+'" id="'+that.a+'" class="'+that.a+'_item mr10"><option value="">省</option></select>'
            +'<select name="'+that.b+'" id="'+that.b+'" class="'+that.b+'_item mr10"><option value="">市</option></select>'
            +'<select name="'+that.c+'" id="'+that.c+'" class="'+that.c+'_item"><option value="">县</option></select>';
            t.html(arthtml);
            var wrap=$(t),
            province=t.find("."+that.a+"_item"),
            city=t.find("."+that.b+"_item"),
            area=t.find("."+that.c+"_item");

            //只显示中国================
            //var ThatData=LocalData[that.name][0][1].child;
            var ThatData=LocalData[that.name][0];
            //console.log(ThatData);
            //显示全部
            //var ThatData=LocalData[that.name][0];
            var tdataB=[],tdataC=[];
            for(var i in ThatData){
                province.append('<option value="'+ThatData[i].area_code+'" data-path="'+ThatData[i].area_code+'">'+ThatData[i].short+'</option>');
            }

            function gethtml(s,id){
                if(s==1){
                    tdataB=ThatData[id].child;
                    for(var i in tdataB){
                        city.append('<option value="'+tdataB[i].area_code+'" data-path="'+tdataB[i].area_code+'">'+tdataB[i].short+'</option>');
                    }
                }
                if(s==2){
                    tdataC=tdataB[id].child;
                    for(var i in tdataC){
                        area.append('<option value="'+tdataC[i].area_code+'" data-path="'+tdataC[i].area_code+'">'+tdataC[i].short+'</option>');
                    }
                }
            };

            function selects(){
                $(that.input).val('');
                if(area.val()){
                    var c=area.find('option:selected');
                    $(that.input).val(c.data('path'));
                    return false;
                }
                if(city.val() && (that.type==0 || typeof(tdataB[city.val()].child)=='undefined')){
                    var b=city.find('option:selected');
                    $(that.input).val(b.data('path'));
                    return false;
                }
                if(province.val() && that.type==0){
                    var a=province.find('option:selected');
                    $(that.input).val(a.data('path'));
                    return false;
                }
            };

            initdb();
            /*初始化数据===============================*/
            function initdb(){
                var input=$(that.input).val();
                var idsarr=[];
                if(input != '' && /[1-9][0-9]{5}/.test(input)){
                    input = input.toString();
                    var pro_code = input.substr(0,2)+'0000';
                    var city_code = input.substr(0,4)+'00';
                    var couty_code = input;
                    //idsarr = input.split("-");
                    province.find('[value="'+pro_code+'"]').attr('selected',true);
                    gethtml(1,pro_code);
                    city.find('[value="'+city_code+'"]').attr('selected',true);
                    gethtml(2,city_code);
                    area.find('[value="'+couty_code+'"]').attr('selected',true);

                    /*if(idsarr.length>3){
                        province.find('[value="'+pro_code+'"]').attr('selected',true);
                        gethtml(1,pro_code);
                    }
                    if(idsarr.length>4){
                        city.find('[value="'+city_code+'"]').attr('selected',true);
                        gethtml(2,city_code);
                    }
                    if(idsarr.length>5){
                        area.find('[value="'+couty_code+'"]').attr('selected',true);
                    }*/
                    selects();
                }
            };
            var _province,_city,_area;
            $(province).selectbox({w:(that.width?that.width[0]:''),cutstr:(that.cutstr?that.cutstr[0]:''),divclass:that.sclass,callback:function(t){
                city.children(':not(:first)').remove();
                area.children(':not(:first)').remove();
                _city.init();
                _area.init();
                var area_code=t.attr('v');
                if(area_code==='') return false;
                gethtml(1,area_code);
                selects();
                _city.init();
                _area.init();
            }},function(){                
                _province=this;                
            });
            $(city).selectbox({w:(that.width?that.width[1]:''),cutstr:(that.cutstr?that.cutstr[1]:''),divclass:that.sclass,callback:function(t){
                area.children(':not(:first)').remove();
                _area.init();
                var area_code=t.attr('v');
                if(area_code==='')return false;
                gethtml(2,area_code);
                selects();
                _area.init();
            }},function(){
                _city=this;                
            });
            $(area).selectbox({w:(that.width?that.width[2]:''),cutstr:(that.cutstr?that.cutstr[2]:''),divclass:that.sclass,callback:function(t){
                selects();
            }},function(){
                _area=this;                
            });

            t.init = function () {
                initdb();
                _province.init();
                _city.init();
                _area.init();
            };
            if($.isFunction(callback)) callback.call(t);
        });
    };
    $.fn.areaB.defaults = {a:'province', b:'city', c:'area',type:0,sclass:''};
}));